class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def levelOrder(self, root):
        result = []
        self.traverse(root, 0, result)
        return result
    
    def traverse(self, node, level, result):
        if not node:
            return
        
        if level >= len(result):
            result.append([])
        
        result[level].append(node.val)
        
        self.traverse(node.left, level + 1, result)
        self.traverse(node.right, level + 1, result)